package com.apes.fn.scm.retailOrder.service.expense;

import com.alibaba.fastjson.JSONObject;
import com.apes.fn.business.fixedLoss.modle.LossQuotation;
import com.apes.fn.business.fixedLoss.repository.LossQuotationRepository;
import com.apes.scm.masterdata.enumeration.model.BusinessParam;
import com.apes.scm.masterdata.enumeration.repository.BusinessParamRepository;
import com.apes.fn.scm.retailOrder.model.RetailOrder;
import com.apes.framework.api.SpringManager;
import com.apes.scm.masterdata.enumeration.model.Enumeration;
import com.apes.scm.masterdata.frame.model.Dept;
import com.apes.scm.masterdata.party.model.PartyRole;

import java.util.List;

/**
 * 赋能门店接车赋能基地施工
 *
 * @author wuq
 * @create 2020-04-20 19:08
 */
public class FnStore2FnBase extends AbstractRetailOrderFnExpense {

    private PartyRole platformCompany;
    private PartyRole fnBase;
    private PartyRole fnStore;
    private Dept dept;
    private Dept serviceDept;
    private RetailOrder retailOrder;

    public FnStore2FnBase(PartyRole platformCompany, PartyRole fnBase, PartyRole fnStore,
                          double amount, String origin, String originBusinessType, String source, String sourceBusinessType,
                          Dept dept,  Dept serviceDept, String carLicense, Enumeration caseType) {
        super(amount, origin, originBusinessType, source, sourceBusinessType, carLicense, caseType);
        this.platformCompany = platformCompany;
        this.fnBase = fnBase;
        this.fnStore = fnStore;
        this.dept = dept;
        this.serviceDept = serviceDept;
    }

    public FnStore2FnBase(PartyRole platformCompany, RetailOrder retailOrder){
        this(platformCompany, retailOrder.getServiceDept().getCompany(), retailOrder.getGetCarDept().getCompany(), retailOrder.getAmtTotal(),
                retailOrder.getSource(), retailOrder.getSourceBusinessType(), retailOrder.getId(), retailOrder.getClass().getSimpleName(),
                retailOrder.getDept(), retailOrder.getServiceDept(), retailOrder.getCustomerExpand().getCarLicense(), retailOrder.getCaseType());
        this.retailOrder = retailOrder;
    }

    @Override
    public void create(JSONObject condition) {
        String type = getCaseType(super.getCaseType().getId());
        double lossQuotationExpense = 0d;   // 定损费
        double infoExpense = 0d;            // 接案介绍费
        switch (type) {
            case "code":    // 代码单
                infoExpense = condition.getDouble(ExpenseEnum.codeOrderInfoExpense.getCode());
                lossQuotationExpense = condition.getDouble(ExpenseEnum.codeOrderLossQuotationExpense.getCode());
                break;
            case "notCode":    // 非代码单
                infoExpense = condition.getDouble(ExpenseEnum.notCodeOrderInfoExpense.getCode());
                lossQuotationExpense = condition.getDouble(ExpenseEnum.notCodeOrderLossQuotationExpense.getCode());
                break;
            case "push":    // 推送
                infoExpense = condition.getDouble(ExpenseEnum.pushOrderInfoExpense.getCode());
                lossQuotationExpense = condition.getDouble(ExpenseEnum.pushOrderLossQuotationExpense.getCode());
                break;
        }
        createInfoExpense(infoExpense);
        createLossQuotationExpense(lossQuotationExpense);
        platformBackBaseExpense(infoExpense);   // 老切新之后执行
        createPlatformExpense(infoExpense);
    }

    @Override
    public void createInfoExpense(double expense) {
        String infoCode = ExpenseEnum.infoExpense.getCode();

        // 判断是否为内部接车
        if (dept.getCompany().getId().equals(serviceDept.getCompany().getId())) {
            infoCode = ExpenseEnum.innerInfoExpense.getCode();
        }

        // 去定损单上面的比率分成
        LossQuotation lossQuotation = SpringManager.getBean(LossQuotationRepository.class).findOne(this.getOrigin());
        expense = lossQuotation.getDividendStore();

        createExpense(fnBase, fnStore, super.getAmount() * expense, infoCode, dept, serviceDept, "");

        createInfoExpenseByHelpReported(infoCode);   // 基地代报案，门店退还基地费用
    }

    private void createInfoExpenseByHelpReported(String infoCode) {
        if (!retailOrder.isHelpReported()) return;
        List<BusinessParam> businessParams = SpringManager.getBean(BusinessParamRepository.class).findAllByQueryKey("helpReported");
        if(businessParams.isEmpty()) throw new RuntimeException("未定义基地代报案，门店退还系数");
        double ratio = Double.parseDouble(businessParams.get(0).getValue());
        createExpense(fnStore, fnBase, super.getAmount() * ratio, infoCode, serviceDept, dept, "基地代报案，门店退还基地5个点的接案介绍费用");
    }

    @Override
    public void createPlatformExpense(double expense) {
        // 平台管理费
        double storePlatformFreeRatio = getPlatformFreeRatio(fnStore);    // 获取门店平台管理费率
        double basePlatformFreeRatio = getPlatformFreeRatio(fnBase);    // 获取基地平台管理费率

        // 取定损单上面的比率分成，由于定损报价单上面会修改这个比率
        LossQuotation lossQuotation = SpringManager.getBean(LossQuotationRepository.class).findOne(this.getOrigin());
        expense = lossQuotation.getDividendStore();

        createExpense(fnBase, platformCompany, super.getAmount() * basePlatformFreeRatio, ExpenseEnum.platformExpense.getCode(), null, serviceDept, "");
        createExpense(fnStore, platformCompany, super.getAmount() * expense * storePlatformFreeRatio, ExpenseEnum.platformExpense.getCode(), null, dept, "");
    }


    private void platformBackBaseExpense(double infoExpense){
        double basePlatformFreeRatio = getPlatformFreeRatio(fnBase);    // 获取基地平台管理费率

        double backBaseExpense = super.getAmount() * infoExpense * basePlatformFreeRatio;
        createExpense(platformCompany, fnBase, backBaseExpense, ExpenseEnum.platformExpense.getCode(), serviceDept, null, "退还基地20%平台费用");
    }

    @Override
    public void createLossQuotationExpense(double expense) {
        createExpense(fnBase, platformCompany, super.getAmount() * expense, ExpenseEnum.lossQuotationExpense.getCode(), null, serviceDept, "");

        // 根据定损费按照平台服务费率退换给基地
        double platformFreeRatio = getPlatformFreeRatio(fnBase);    // 获取基地平台管理费率
        createExpense(platformCompany, fnBase, super.getAmount() * expense * platformFreeRatio, ExpenseEnum.platformExpense.getCode(), serviceDept, null, "平台退回基地定损信息费的平台服务费。");

    }
}
